/**
 * copyright(c) 2013 www.jiongshibaoliao.com, All Rights Reserved.
 * project: jiongshibaoliao 
 * author: 曾帅
 * date: 2014-1-2 上午10:23:39
 * version: v1.0
 */

package com.jiongshibaoliao.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import com.jiongshibaoliao.common.constants.Constants;
import com.jiongshibaoliao.exception.JiongException;

/**
 * @project jiongshibaoliao
 * @author 曾帅
 * @version 1.0
 * @date 2014-1-2 上午10:23:39
 */

public class JiongMD5 {
	
	/**
	 * MD5加密
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-1-2 上午11:32:13
	 * @param lawsStr
	 * @return
	 * @throws JiongException String
	 */
	public static String toMD5(String lawsStr) throws JiongException {
		if (StringUtils.isBlank(lawsStr)) {
			throw new JiongException("加密失败！");
		}

		lawsStr = JiongMD5.confuse(lawsStr);
		lawsStr = DigestUtils.md5Hex(lawsStr);
		return lawsStr;
	}

	/**
	 * 打乱原始串
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-1-2 上午11:32:17
	 * @param originalStr
	 * @return
	 * @throws JiongException String
	 */
	private static String confuse(String originalStr) throws JiongException {
		if (StringUtils.isBlank(originalStr)) {
			throw new JiongException("原始串异常！");
		}
		
		originalStr = Constants.MD5_HEAD_STR + originalStr;
		String confuseStr = "";
		for(int i = 0; i < originalStr.length(); i ++) {
			confuseStr += (originalStr.substring(originalStr.length() - i - 1, originalStr.length() - i));
		}
		
		return confuseStr;
	}

}
